const { defineConfig } = require('@vue/cli-service');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin


module.exports = defineConfig({
  transpileDependencies: true,
  configureWebpack: {
    plugins: [
      new BundleAnalyzerPlugin()
    ],

    optimization: {
      splitChunks: {
        chunks: 'all',
        minSize: 30000,
        maxSize: 0,
        minChunks: 1,
        maxAsyncRequests: 5,
        maxInitialRequests: 3,
        cacheGroups: {
          echarts: {
            name: 'echarts',
            test: '/[\\/]node_modules[\\/]echarts[\\/]/',
            priority: 20,
          },
        },
      },
    },
  },

  chainWebpack: config => {
    config
      .when(process.env.NODE_ENV === 'production', // 配置生产环境生效
        config => {
          config.optimization.splitChunks({
            chunks: 'all', // 将对什么类型的代码进行分割，三种值：all: 全部 ｜ async: 异步，按需加载的代码 ｜ initial: 入口代码块
            cacheGroups: { // 缓存组
              // 定义 libs 缓存组，分割从 node_modules 中引入的代码
              // libs: {
              //   name: 'chunk-libs', // 分割成的文件名
              //   test: /[\\/]node_modules[\\/]/, // 匹配 node_modules 中模块
              //   priority: 10, // 优先级，当模块同时命中多个缓存组的规则时，分配到优先级高的缓存组
              //   chunks: 'initial' // 这里覆盖上面的 chunks: 'all'，仅打包最初依赖的第三方库
              // },
              echarts: {
                name: 'echarts',
                test: '/[\\/]node_modules[\\/]echarts[\\/]/',
                priority: 20,
              },
            }
          })
        }
      )
  }
});
